package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EnterpriseUser
import cn.exrick.xboot.base.entity.Job
import cn.exrick.xboot.base.mapper.JobMapper
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.IJobService
import cn.exrick.xboot.base.vo.JobVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import javax.annotation.Resource

/**
 * 科室接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IJobServiceImpl : ServiceImpl<JobMapper?, Job?>(), IJobService {


    @Autowired
    private lateinit var securityUtil:SecurityUtil

    @Autowired
    private lateinit var  iEnterpriseUserService: IEnterpriseUserService

    override fun createOrUpdateJob(job: Job?): Result<Any?>? {

        return job?.run {

            enterpriseId = iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id)
            saveOrUpdate(job)
           return ResultUtil.success("添加或者更新成功！")
        } ?: ResultUtil.error("参数缺失")
    }

    override fun removeJob(ids: Array<out String>): Result<Any?>? {

        removeByIds(ids.toList())
        return ResultUtil.success("移除成功")
    }

    override fun getJobList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

       return ToolUtil.setListToNul(list(KtQueryWrapper(Job::class.java).eq(Job::enterpriseId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(Job::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()

            result["size"] = this.size
            result["content"] = PageUtil.listToPage(pageVo, this)
            ResultUtil.data(result, "获取岗位列表成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun jobVoList(): Result<MutableList<JobVo>?>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(Job::class.java).eq(Job::enterpriseId, iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(Job::createTime)))?.run {

            ResultUtil.data( this.map {
                JobVo().apply { ToolUtil.copyProperties(it, this) }
            }.toMutableList())
        }?: ResultUtil.error(201,"暂无数据")
    }

    override fun getJobVoListByEnterpriseId(enterpriseId: String): Result<MutableList<JobVo>>? {

           return ToolUtil.setListToNul(list(KtQueryWrapper(Job::class.java)
                   .eq(Job::enterpriseId, enterpriseId)
                   .orderByDesc(Job::createTime)))?.let {

                ResultUtil.data(it.map { JobVo().apply { ToolUtil.copyProperties(getById(it!!.id), this) } }.toMutableList(), "获取数据成功")
            } ?: ResultUtil.error(201,"暂无数据")
    }

    override fun getJobName(id: String): String? {

        return getById(id)?.run { jobName } ?: "-"
    }
}
